<?php
/** 初始化项目 */
require '../../app/app.php';
/** 初始化数据库 */
use app\util\Mysql;
$database = Mysql::init();

$adname = $_SESSION["username"];
$client_ip = getClientIP();
$timestamp = time();
if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]) && isset($_POST["email"]) && isset($_POST["imgurl"]) && isset($_POST["group"]) && isset($_POST["adming"]) && isset($_POST["open"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $imgurl = $_POST["imgurl"];
    $group = $_POST["group"];
    $adming = $_POST["adming"];
    $open = $_POST["open"];
    $uid = $_POST["uid"];
    $repname = $database->select("user_text", "id", ["username" => $username]);

    if ($repname[0] != null && $uid != $repname[0]) {
        echo "用户名重复!";
        // echo $repname[0];
        exit();
    } else if ($open == "on") {
        if ($imgurl == NULL) {
            $imgurl = "/data/upload/img/default.png";
        }
        if ($group == "") {
            $group = $database->select("user_text", "gid", ["id" => $uid]);
            $group = $group[0];
        }
        $open = "是";
        $cgaracrers = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $salt = '';
        for ($i = 0; $i < 6; $i++) {
            $salt .= $cgaracrers[rand(0, strlen($cgaracrers) - 1)];
        }
        if ($password == "") {
            $database->update("user_text", ["username" => $username, "phone" => $phone, "email" => $email, "imgurl" => $imgurl, "gid" => $group, "sysgrade" => $adming, "static" => $open], ["id" => $uid]);
            $content = $adname."修改了用户：".$username."的信息";
            $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);
            echo "修改成功";

            exit();
        } else {
            $encrypted_data1 = md5($password);
            $encrypted_data2 = hash("sha512", $encrypted_data1 . $salt);
            $encrypted_data = hash("sha512", $encrypted_data2);
            $database->update("user_text", ["username" => $username, "password" => $encrypted_data, "phone" => $phone, "email" => $email, "imgurl" => $imgurl, "gid" => $group, "sysgrade" => $adming, "static" => $open], ["id" => $uid]);
            $database->update("pass_key", ["salt" => $salt], ["uid" => $uid]);
            $content = $adname."修改了用户：".$username."的信息";
            $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);

            echo "修改成功";
            exit();
        }
    } else {
        echo "参数错误！";
    }
} else if (isset($_POST["username"]) && isset($_POST["password"]) && isset($_POST["phone"]) && isset($_POST["email"]) && isset($_POST["imgurl"]) && isset($_POST["group"]) && isset($_POST["adming"])) {
    $username = $_POST["username"];
    $password = $_POST["password"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $imgurl = $_POST["imgurl"];
    $group = $_POST["group"];
    $adming = $_POST["adming"];
    $uid = $_POST["uid"];
    $repname = $database->select("user_text", "id", ["username" => $username]);
    if ($repname[0] != null && $uid != $repname[0]) {
        echo "用户名重复!";
        echo $repname[0];
        exit();
    } else {
        if ($imgurl == NULL) {
            $imgurl = "/data/upload/img/default.png";
        }
        if ($group == "") {
            $group = $database->select("user_text", "gid", ["id" => $uid]);
            $group = $group[0];
        }
        $open = "否";
        $cgaracrers = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $salt = '';
        for ($i = 0; $i < 6; $i++) {
            $salt .= $cgaracrers[rand(0, strlen($cgaracrers) - 1)];
        }
        if ($password == "") {
            $database->update("user_text", ["username" => $username, "phone" => $phone, "email" => $email, "imgurl" => $imgurl, "gid" => $group, "sysgrade" => $adming, "static" => $open], ["id" => $uid]);
            $content = $adname."修改了用户：".$username."的信息";
            $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);

            echo "修改成功";
            exit();
        } else {
            $encrypted_data1 = md5($password);
            $encrypted_data2 = hash("sha512", $encrypted_data1 . $salt);
            $encrypted_data = hash("sha512", $encrypted_data2);
            $database->update("user_text", ["username" => $username, "password" => $encrypted_data, "phone" => $phone, "email" => $email, "imgurl" => $imgurl, "gid" => $group, "sysgrade" => $adming, "static" => $open], ["id" => $uid]);
            $database->update("pass_key", ["salt" => $salt], ["uid" => $uid]);
            $content = $adname."修改了用户：".$username."的信息";
            $database->insert("secure_info", ["user" => $adname, "ip" => $client_ip, "times" => $timestamp, "content" => $content]);

            echo "修改成功";
            exit();
        }
    }
} else {
    echo "参数错误！";
}
